﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZM.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZM.UI
{
    /// <summary>
    /// 首页-材料消耗统计
    /// </summary>
    /// Title:ZM9W，ServiceZM9W
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2012-12-06  高强   [创建]
    ///
    public partial class ZM9W : EFViewCmes
    {
        public ZM9W()
        {
            InitializeComponent();
        }

        /// <summary>
        //初始化并渲染控件
        /// </summary>
        public override void Initialize(Baosight.Platform.Core.EIInfo initData)
        {
            base.Initialize(initData);
            this.RATEDVALUE.Text = initData.Get("RATEDVALUE").ToString();
            this.COMSUMEVALUE.Text = initData.Get("COMSUMEVALUE").ToString();
            EIBlock block = initData.GetBlock("result");
            if (block.Rows.Count <= 0)
            {
                return;
            }

            Chart.DefaultSeriesDefinition.ShowItemToolTips = true;
            Chart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;

            Chart.DefaultView.ChartArea.AdditionalYAxes.Clear();

            Chart.DefaultView.ChartArea.AxisY.MajorGridLinesVisibility = Visibility.Visible;
            Chart.DefaultView.ChartArea.AxisY.MinorGridLinesVisibility = Visibility.Visible;
            Chart.DefaultView.ChartArea.AxisY.StripLinesVisibility = Visibility.Visible;
            Chart.DefaultView.ChartArea.AxisY.DefaultLabelFormat = Common.ChartDefaultLabelFormat;
            Chart.DefaultView.ChartLegend.Header = "";
            Chart.SeriesMappings.Clear();
            SeriesMapping sm1 = Common.AddBarToChart("额定额度", 0);
            SeriesMapping sm2 = Common.AddBarToChart("消耗量", 1);
            sm1.SeriesDefinition.ItemLabelFormat = Common.ChartItemLabelFormat;
            sm2.SeriesDefinition.ItemLabelFormat = Common.ChartItemLabelFormat;
            Chart.SeriesMappings.Add(sm1);
            Chart.SeriesMappings.Add(sm2);

            StackPanel spLegendHeader = new StackPanel();
            spLegendHeader.Orientation = Orientation.Horizontal;
            TextBlock tbLegendHeader = new TextBlock();
            tbLegendHeader.Text = "本月各部门材料消耗";
            tbLegendHeader.Foreground = new SolidColorBrush(Colors.White);
            tbLegendHeader.HorizontalAlignment = HorizontalAlignment.Center;
            spLegendHeader.Children.Add(tbLegendHeader);

            //使tbLegendHeader.Text位于正中央
            TextBlock tbForAlignment = new TextBlock();
            tbForAlignment.Width = 120;
            spLegendHeader.Children.Insert(0, tbForAlignment);

            List<List<ChartData>> itemsSource = new List<List<ChartData>>();
            List<ChartData> lstPlan = new List<ChartData>();
            List<ChartData> lstActual = new List<ChartData>();

            //使用Dictionary帮助后面填充值
            Dictionary<string, ChartData> dicPlan = new Dictionary<string, ChartData>();
            Dictionary<string, ChartData> dicActual = new Dictionary<string, ChartData>();

            foreach (var item in block.Rows)
            {
                if (item["departmentName"].ToString().Trim() == string.Empty)
                {
                    continue;
                }
                ChartData cdPlan = new ChartData(item["departmentName"].ToString(), Common.GetDecimalValue(item["intialNation"]));
                ChartData cdActual = new ChartData(item["departmentName"].ToString(), Common.GetDecimalValue(item["SUM"]));

                lstPlan.Add(cdPlan);
                lstActual.Add(cdActual);
            }


            itemsSource.Add(lstPlan);
            itemsSource.Add(lstActual);

            Chart.ItemsSource = itemsSource;
        }
    }
}
